<?php
/**
 * Class FormRegistration created by Pixoid.
 *
 * @version 1.0
 */
class FormRegistration extends User {

    public $config = array();
    public $password_repeat;
    public $captcha;
    public $sogl;

    public function init() {
        $this->config = array(
            'class' => 'form',
            'elements' => array(
                'username' => array('type' => 'text'),
                'email' => array('type' => 'text'),
                'password' => array('type' => 'password'),
                'password_repeat' => array('type' => 'password'),
                'country' => array('type' => 'text'),
                'town' => array('type' => 'text'),
                'telephone' => array('type' => 'text'),
                'captcha' => array('type' => 'MyCaptcha', 'clickableImage' => true, 'showRefreshButton' => false),
                'sogl' => array('type' => 'checkbox', 'layout' => "<br/>{input} {label}\n{hint}\n{error}")
            ),
            'buttons' => array(
                'registration' => array('type' => 'htmlSubmit', 'value' => 1, 'class' => ' blue', 'label' => '<span class="label">Зарегистрироваться</span>'),
                'login' => array('type' => 'link', "class" => "right_alight", 'label' => 'Вход', 'href' => "/user/login"),
                'recover' => array('type' => 'link', "class" => "right_alight", 'label' => 'Забыли пароль?', 'href' => "/user/recover"),
            )
        );
        return parent::init();
    }

    public function rules() {
        return array(
            array('username, country, town, telephone', 'length', 'max' => 60, 'min' => 3),
            array('password', 'length', 'max' => 128, 'min' => 6),
            array('telephone', 'match', 'pattern' => '/^[0-9_]+$/u'),
            array('password_repeat', 'compare', 'compareAttribute' => 'password'),
            array('email', 'email'), array('email', 'unique'),
            array('country, town, telephone', 'safe'),
            array('town', 'safe'),
            array('username, email, password, password_repeat', 'required'),
            array('sogl', 'required', 'requiredValue' => true, 'message' => 'Условия придется принять, иначе пользование сайтом невозможно'),
            array('captcha', 'captcha', 'allowEmpty' => !extension_loaded('gd')),
            array('password_repeat', 'registration'),
        );
    }

    public function attributeLabels() {
        return array(
            'username' => 'Имя пользователя',
            'password' => 'Пароль',
            'password_repeat' => "Повторите пароль",
            'email' => 'E-mail',
            'captcha' => 'Капча',
            'country' => 'Страна',
            'town' => 'Город',
            'telephone' => 'Телефон',
            'sogl' => 'Я ознакомился с условиями регистрации'
        );
    }

    public function registration($attribute, $params) {
        if(!$this->hasErrors()) {
            $this->code = $this->generateActivationKey(true, $this->password);
            $email = Yii::app()->email;
            $email->to = $this->email;
            $email->from = Yii::app()->params['adminEmail'];
            $email->message = Yii::app()->controller->renderPartial('../email/registration', array('model' => $this), true);
            $email->subject = 'Регистрация';
            $email->send();
            $this->password = User::encrypt($this->password);
            $this->password_repeat = User::encrypt($this->password_repeat);
            $this->save(false);
        }
    }

}
